package cc.telecomdigital.tdstock.activity.groups.more.push.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import cc.mango.data.DataHelper;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.activity.groups.monitor.util.HttpHelper;

/**
 * 
 * Project：Server_Push Package：cc.mango.test.dataDemo.util Class：XMLRead
 * Description： Creater：hunter CreateTime：Oct 10, 2011 3:13:18 PM Modifier
 * ：hunter Modify time：Oct 10, 2011 3:13:18 PM Modify Remarks： read data
 * 
 * @version 1.0
 * 
 */
public class DataRead
{
	public static final String	TAG	= DataRead.class.getSimpleName();

	/**
	 * get Content form url
	 * 
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static synchronized String getContentFromURL(String url)
	{
		/*
		 * String _url_temp=HttpHelper.isServerAvaiable(url, context, num);
		 * TDStockLog.d(TAG, _url_temp==null?"_url_temp is null":_url_temp);
		 * if(_url_temp==null){ return null; }else{
		 */
		DefaultHttpClient client = new DefaultHttpClient();
		HttpParams httpParams = client.getParams();
		HttpConnectionParams.setConnectionTimeout(httpParams,
				HttpHelper.TIMEOUT_FOR_CONNECTION);
		HttpConnectionParams.setSoTimeout(httpParams,
				HttpHelper.TIMEOUT_FOR_READ);
		client.setParams(httpParams);
		HttpPost post = new HttpPost(url);
		ResponseHandler<String> ha = new BasicResponseHandler();
		/* HttpResponse response=client.execute(post); */
		String temp = null;
		try
		{
			temp = client.execute(post, ha);
			if (temp != null
					&& temp.indexOf(DataHelper.SESSION_EXPIRED_MSG) != -1)
			{
				temp = null;
			}
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			temp = null;
		}
		TDStockLog.d(TAG, temp == null ? "" : temp);
		return temp;
		// }
	}

	/*
	 * public static String getContentFromURL(String url) { String s = null;
	 * URLConnection conn=null; try { URL u = new URL(url); conn=
	 * u.openConnection(); conn.setConnectTimeout(8000);
	 * conn.setReadTimeout(10000); s =
	 * DataRead.InputStreamConvertToString(conn.getInputStream());
	 * TDStockLog.d(TAG, s==null?"":s); } catch (Exception e) { // TODO
	 * Auto-generated catch block e.printStackTrace(); }finally{ if(conn!=null){
	 * conn=null; }
	 * 
	 * }
	 * 
	 * return s; }
	 */
	/**
	 * InputStream convert to String
	 * 
	 * @param in
	 * @return String
	 * @throws IOException
	 * @throws IOException
	 */
	/*
	 * public static String InputStreamConvertToString(InputStream in) throws
	 * IOException{ BufferedReader br=new BufferedReader(new
	 * InputStreamReader(in)); String strs=""; String s=br.readLine();
	 * while(s!=null){ strs=strs+s; s=br.readLine(); } br.close(); return strs;
	 * }
	 */

	/**
	 * get InputStream From url
	 * 
	 * @param url
	 * @return
	 */
	public static InputStream getInputStreamFromURL(String url)
	{
		InputStream in;
		try
		{
			String temp = getContentFromURL(url);
			TDStockLog.d(TAG, temp == null ? "temp is null" : temp);
			if (temp == null)
			{
				in = null;
			}
			else
			{
				in = new ByteArrayInputStream(temp.getBytes());
			}
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			in = null;
		}
		TDStockLog.d(TAG, in == null ? "is null" : in.toString());
		return in;
	}
	/*
	 * public static InputStream getInputStreamFromURL(String url) {
	 * URLConnection conn=null; try { URL u = new URL(url); conn =
	 * u.openConnection(); conn.setConnectTimeout(CONNECTION_TIMEOUT);
	 * conn.setReadTimeout(READ_TIMEOUT); return conn.getInputStream(); } catch
	 * (Exception e) { // TODO Auto-generated catch block e.printStackTrace();
	 * return null; } }
	 */
	// /**
	// * 通过URL获取指定股票最新新闻的相关信息
	// * @param url
	// * @throws IOException
	// */
	// public static void read_News_List(String url) throws IOException{
	// InputStream in=getInputStreamFromURL(url);
	// if(in==null){
	// throw new
	// IOException(DataRead.class.getSimpleName()+"--------------->InputStream  Is  null");
	// }
	// Document doc=XMLHelper.parseURL(getInputStreamFromURL(url) );
	// String id=XMLHelper.findNodeContent(doc, "ID");
	// if(Data.id!=null&&id!=null&&!id.equals("")){
	// Data.id=id;
	// Data.status=XMLHelper.findNodeContent(doc, "Status");
	// Data.datetime=XMLHelper.findNodeContent(doc, "DateTime");
	// Data.vendor=XMLHelper.findNodeContent(doc, "Vendor");
	// Data.title=XMLHelper.findNodeContent(doc, "Title");
	// }else {
	// throw new
	// IOException(DataRead.class.getSimpleName()+"--------------->get  news id is not  undown");
	// }
	//
	// }
	/**
	 * 通过URL获取指定消息相关信息
	 * 
	 * @param url
	 * @throws IOException
	 */
	/*
	 * public static void getMessageContent(String url,Context context,int num)
	 * throws IOException{ String in=getContentFromURL(url,context,num);
	 * if(in==null){ throw new IOException(DataRead.class.getSimpleName()+
	 * "--------------->InputStream  Is  null"); } InputStream
	 * in=getInputStreamFromURL(url,context,num); if(in==null){ Data.id="";
	 * Data.status=""; Data.dateTime=""; Data.vendor=""; Data.title="";
	 * Data.content=""; }else{ Document doc=XMLHelper.parseURL(in); Map<String,
	 * String> maps=XMLHelper.getXMLContent(doc); for(String key:maps.keySet()){
	 * if("ID".equals(key)){ Data.id=maps.get(key); }else
	 * if("Status".equals(key)){ Data.status=maps.get(key); }else
	 * if("DateTime".equals(key)){ Data.dateTime=maps.get(key); }else
	 * if("Vendor".equals(key)){ Data.vendor=maps.get(key); }else
	 * if("Title".equals(key)){ Data.title=maps.get(key); }else
	 * if("Content".equals(key)){ Data.content=maps.get(key); } } } }
	 */
	/*
	 * public static void getMessageList(String url,Context context,int num)
	 * throws IOException{ TDStockLog.d(TAG, url==null?"url is null":url);
	 * String temp=DataRead.getContentFromURL(url,context,num);
	 * Data.message_list_html=temp==null?"":temp; }
	 */
}
